clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"

clear all
set more off
set type double

cap log close

global figures = "Results/Figures"
global output = "Results/Output"
global revisionoutput = "Results/RevisionRFS/Output"

****************************************************************************************************
//Second Stage
****************************************************************************************************


/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2.dta", clear

/* get rid of observations with observed life span less than 10% */
drop if observed_life_span <= 0.5

/* get rid of observations with observed holding less than 30% */
drop if observed_holding_mean <= 0.25

/* get rid of observations with time to maturity > 25 years */
 drop if timetomat >= 25 

gen coupon_dec = coupon/100

gen mp = price_eom/100
gen ln_mp  =ln(mp)


/* Generate instrument from pooled first stage */

gen yieldhat = .

reghdfe yield IVme_alt6_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==0, a(date) clus(date) resid

predict yieldhat_type, xbd

replace yieldhat = yieldhat_type if type_new2==0
	
drop yieldhat_type

forval i=1(1)7{
	
	reghdfe yield IVme_alt6_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) resid
	
	predict yieldhat_type, xbd
	
	replace yieldhat = yieldhat_type if type_new2==`i'
	
	drop yieldhat_type

}

gen fundid_ac = fundid 
gen aum_assetclass_ac = aum_assetclass
gegen bin_aum_ac = sum(aum_assetclass), by(bin date assetclass)

replace aum_assetclass_ac = bin_aum_ac if type_new2 == 5
replace fundid_ac = bin if type_new2 == 5


label var yieldhat "$\overline{\textit{Yield}}_{n,t}$"  
label var LNtimetomat "$ \textit{Maturity}_{n,t}$" 
label var spread "$\textit{Bid-Ask}_{n,t}$"
label var rating_num "$\textit{Rating}_{n,t}$" 
label var LNbv "$ \textit{Issuance Size}_{n,t}$" 
label var coupon_dec "$\textit{Coupon}_{n,t}$" 

/* Save non-standardized data */
save "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_non_std.dta", replace



/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

********************************************************************************
/* 1. Step: Pooled Regression */
********************************************************************************

/* Estimation */
forval i=1(1)7{
preserve
keep if type_new2==`i'

eststo: ivreghdfe LNrweight (yield = IVme_alt6_95) LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) nocollin 
 	estadd local fund_time_fe "Yes"
	
	
restore
}

/* Reporting */
esttab using "${revisionoutput}/pooled_results_alt6", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yield LNtimetomat spread LNbv rating_num) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear


********************************************************************************
/* 2. Step: Within Assetclass Regressions */
********************************************************************************

/* Estimation */
forval i=1(1)7{
preserve
keep if type_new2==`i' 

	eststo: ivreghdfe LNrweight_conditional (yield = IVme_alt6_95) LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 1 [aw=aum_assetclass_ac], a(date#fundid_ac) clus(fundid_ac) nocollin  resid
	estadd local fund_time_fe "Yes"
	
	predict deltahat_1, xbd
	
	replace deltahat_1 =. if assetclass == 2
	
	eststo: ivreghdfe LNrweight_conditional (yield = IVme_alt6_95) LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 2 [aw=aum_assetclass_ac], a(date#fundid_ac) clus(fundid_ac) nocollin resid
 	estadd local fund_time_fe "Yes"
	
	predict deltahat_2, xbd
	
	replace deltahat_2 =. if assetclass == 1
	
	gen LNdeltahat = deltahat_1
	replace LNdeltahat = deltahat_2 if LNdeltahat ==. 
	
	keep date fundid bondid assetclass LNdeltahat
	
	save "${tempdata}/TMP_`i'_conditional_alt6", replace

restore
}

/* Reporting */
esttab using "${revisionoutput}/conditional_results_alt6", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yield LNtimetomat spread LNbv rating_num) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear


preserve
********************************************************************************
/* 3. Step: Construct Instruments */
********************************************************************************

/* Load Data for Instrument */
use "${tempdata}/TMP_1_conditional_alt6", clear
append using "${tempdata}/TMP_2_conditional_alt6"
append using "${tempdata}/TMP_3_conditional_alt6"
append using "${tempdata}/TMP_4_conditional_alt6"
append using "${tempdata}/TMP_5_conditional_alt6"
append using "${tempdata}/TMP_6_conditional_alt6"
append using "${tempdata}/TMP_7_conditional_alt6"

gsort fundid date assetclass bondid

gen deltahat = exp(LNdeltahat)
gegen sum_deltahat = sum(deltahat), by(fundid date assetclass)

replace sum_deltahat = . if sum_deltahat ==0
gen LNoutweight_conditional_hat = ln(1+sum_deltahat)  



save "${tempdata}/TMP_instruments_alt61", replace

restore
********************************************************************************
/* 4. Step: Across Assetclass Regression */
********************************************************************************
 
merge 1:1 date fundid bondid assetclass using "${tempdata}/TMP_instruments_alt61"

duplicates drop date fundid assetclass, force

keep date fundid assetclass outweight_conditional LNoutweight_conditional_hat aum_assetclass aum_tot type_new2

gegen id = group(date fundid)
reshape wide outweight_conditional LNoutweight_conditional_hat aum_assetclass aum_tot, i(id) j(assetclass)


gen LNweight_assetclass = ln(aum_assetclass1/aum_assetclass2)
gen LNoutweight_conditional1 = -ln(outweight_conditional1)
gen LNoutweight_conditional2 = ln(outweight_conditional2)
replace LNoutweight_conditional_hat1 = -LNoutweight_conditional_hat1
replace LNoutweight_conditional_hat2 = LNoutweight_conditional_hat2

/* Estimation */
forval i=1(1)7{
preserve
keep if type_new2==`i'

// eststo: ivreg2 LNweight_assetclass (LNoutweight_conditional1 LNoutweight_conditional2 = LNoutweight_conditional_hat1 LNoutweight_conditional_hat2) [aw=aum_tot1] if type_new2==`i', cl(date)
//	
eststo: ivreghdfe LNweight_assetclass (LNoutweight_conditional1 LNoutweight_conditional2 = LNoutweight_conditional_hat1 LNoutweight_conditional_hat2) [aw=aum_tot1] if type_new2==`i', a(fundid) cl(fundid)
	
	
restore
}

label var LNoutweight_conditional1 "$\textit{Investment Grade}$"  
label var LNoutweight_conditional2 "$ \textit{High Yield}$" 

/* Reporting */
esttab using "${revisionoutput}/across_results_alt6", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N widstat, fmt(0 %9.2f) ///
labels("Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(LNoutweight_conditional1 LNoutweight_conditional2) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear




****************************************************************************************************
// Second Stages with Pooled First Stages
****************************************************************************************************


clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"

clear all
set more off
set type double

cap log close

global figures = "Results/Figures"
global output = "Results/Output"
global revisionoutput = "Results/RevisionRFS/Output"

/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

********************************************************************************
/* 2. Step: Within Assetclass Regressions - Pooled First Stage */
********************************************************************************

preserve

keep if type_new2==0 
	
eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==0 & assetclass == 1, a(date) resid
estadd local fund_time_fe "Yes"
	
predict deltahat_1, xbd
	
replace deltahat_1 =. if assetclass == 2
	
eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==0 & assetclass == 2, a(date) resid
 estadd local fund_time_fe "Yes"
	
predict deltahat_2, xbd
	
replace deltahat_2 =. if assetclass == 1
	
gen LNdeltahat = deltahat_1
replace LNdeltahat = deltahat_2 if LNdeltahat ==. 
	
keep date fundid bondid assetclass LNdeltahat type_new2
	
save "${tempdata}/TMP_0_conditional_alt6_pooled_fs", replace

restore

eststo clear

/* Estimation */
forval i=1(1)7{
preserve
	keep if type_new2==`i' 
	
	eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 1 [aw=aum_assetclass_ac], a(date#fundid_ac) clus(fundid_ac) resid
	estadd local fund_time_fe "Yes"
	
	predict deltahat_1, xbd
	
	replace deltahat_1 =. if assetclass == 2
	
	eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 2 [aw=aum_assetclass_ac] , a(date#fundid_ac) clus(fundid_ac) resid
 	estadd local fund_time_fe "Yes"
	
	predict deltahat_2, xbd
	
	replace deltahat_2 =. if assetclass == 1
	
	gen LNdeltahat = deltahat_1
	replace LNdeltahat = deltahat_2 if LNdeltahat ==. 
	
	keep date fundid fundid_ac bondid assetclass LNdeltahat type_new2
	
	save "${tempdata}/TMP_`i'_conditional_alt6_pooled_fs", replace

restore
}

/* Reporting */
esttab using "${revisionoutput}/conditional_results_alt6_pooled_fs", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yieldhat LNtimetomat spread LNbv rating_num) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear


preserve
********************************************************************************
/* 3. Step: Construct Instruments */
********************************************************************************

/* Load Data for Instrument */
use "${tempdata}/TMP_0_conditional_alt6_pooled_fs", clear
append using "${tempdata}/TMP_1_conditional_alt6_pooled_fs"
append using "${tempdata}/TMP_2_conditional_alt6_pooled_fs"
append using "${tempdata}/TMP_3_conditional_alt6_pooled_fs"
append using "${tempdata}/TMP_4_conditional_alt6_pooled_fs"
append using "${tempdata}/TMP_5_conditional_alt6_pooled_fs"
append using "${tempdata}/TMP_6_conditional_alt6_pooled_fs"
append using "${tempdata}/TMP_7_conditional_alt6_pooled_fs"

gsort fundid date assetclass bondid

gen deltahat = exp(LNdeltahat)
gegen sum_deltahat = sum(deltahat), by(fundid date assetclass)

replace sum_deltahat = . if sum_deltahat ==0
gen LNoutweight_conditional_hat = ln(1+sum_deltahat)  

winsor2 LNoutweight_conditional_hat, replace cuts(1 99) by(date)

save "${tempdata}/TMP_instruments_alt6_pooled_fs", replace

restore

********************************************************************************
/* 4. Step: Across Assetclass Regression */
********************************************************************************
 
merge 1:1 date fundid bondid assetclass using "${tempdata}/TMP_instruments_alt6_pooled_fs"


duplicates drop date fundid assetclass, force

keep date fundid assetclass outweight_conditional LNoutweight_conditional_hat aum_assetclass aum_tot type_new2

gegen id = group(date fundid)
reshape wide outweight_conditional LNoutweight_conditional_hat aum_assetclass aum_tot, i(id) j(assetclass)
	
gen LNweight_assetclass = ln(aum_assetclass1/aum_assetclass2)
gen LNoutweight_conditional1 = -ln(outweight_conditional1)
gen LNoutweight_conditional2 = ln(outweight_conditional2)
replace LNoutweight_conditional_hat1 = -LNoutweight_conditional_hat1
replace LNoutweight_conditional_hat2 = LNoutweight_conditional_hat2
gen weight1 = aum_assetclass1/aum_tot1
gen weight2 = aum_assetclass2/aum_tot1
gen both = 0
replace both = 1 if weight1 > 0.05 & weight2 > 0.05 & type_new2 != 5
replace both = 1 if weight1 > 0.08 & weight2 > 0.08 & type_new2 == 5

gen _b_LNoutweight_conditional1 = .
gen _b_LNoutweight_conditional2 = .
gen _se_LNoutweight_conditional1 = .
gen _se_LNoutweight_conditional2 = .
gen num_obs = .

eststo clear 

capture noisily: ivreghdfe LNweight_assetclass (LNoutweight_conditional1 LNoutweight_conditional2 = LNoutweight_conditional_hat1 LNoutweight_conditional_hat2) [aw=aum_tot1] if type_new2==0, a(type_new2) cl(date)

replace _b_LNoutweight_conditional1 = _b[LNoutweight_conditional1] if type_new2==0
replace _b_LNoutweight_conditional2 = _b[LNoutweight_conditional2] if type_new2==0
replace _se_LNoutweight_conditional1 = _se[LNoutweight_conditional1] if type_new2==0
replace _se_LNoutweight_conditional2 = _se[LNoutweight_conditional2] if type_new2==0
replace num_obs = e(N) if type_new2==0


/* Estimation */
forval i=1(1)7{

	capture noisily eststo: ivreghdfe LNweight_assetclass (LNoutweight_conditional1 LNoutweight_conditional2 = LNoutweight_conditional_hat1 LNoutweight_conditional_hat2) [aw=aum_tot1] if type_new2==`i' & both == 1, absorb(fundid) cl(date)
	
	replace _b_LNoutweight_conditional1 = _b[LNoutweight_conditional1] if type_new2==`i'
	replace _b_LNoutweight_conditional2 = _b[LNoutweight_conditional2] if type_new2==`i'
	replace _se_LNoutweight_conditional1 = _se[LNoutweight_conditional1] if type_new2==`i'
	replace _se_LNoutweight_conditional2 = _se[LNoutweight_conditional2] if type_new2==`i'
	replace num_obs = e(N) if type_new2==`i'

}

preserve 

keep type_new2 fundid date LNweight_assetclass LNoutweight_conditional1 LNoutweight_conditional2 num_obs _b_LNoutweight_conditional1 _b_LNoutweight_conditional2 _se_LNoutweight_conditional1 _se_LNoutweight_conditional2

duplicates drop type_new2 fundid date LNweight_assetclass LNoutweight_conditional1 LNoutweight_conditional2 num_obs _b_LNoutweight_conditional1 _b_LNoutweight_conditional2 _se_LNoutweight_conditional1 _se_LNoutweight_conditional2, force

save "${revisionoutput}/across_assetclass_coeff_se_cf.dta", replace

restore 

label var LNoutweight_conditional1 "$\textit{Investment Grade}$"  
label var LNoutweight_conditional2 "$ \textit{High Yield}$" 

/* Reporting */
esttab using "${revisionoutput}/across_results_alt6_pooled_fs", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N widstat, fmt(0 %9.2f) ///
labels("Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(LNoutweight_conditional1 LNoutweight_conditional2) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear


****************************************************************************************************
// Quarter by Quarter with Pooled First Stages
****************************************************************************************************


clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"

clear all
set more off
set type double

cap log close

global revisionfigures = "Results/RevisionRFS/Figures"
global revisionoutput = "Results/RevisionRFS/Output"


/* Load data */

use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

gen l_p_f = 1 if type_new2==1
replace l_p_f = 3 if type_new2==3 

label define l_p_f_l 1 "Life" 2 "P&C"  3 "Mutual Funds (Traditional)"

label values l_p_f l_p_f_l

tab l_p_f

drop type*

drop if mi(l_p_f)

*************************************************
// Special Case - All Bonds
*************************************************

postutil clear

postfile coeff l_p_f date num_obs f_stat _b_yieldhat _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yieldhat _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_all_qs_pooled_fs.dta", replace
forval i=1(2)3{
		
	forval q=`=tq(2006q1)'(1)`=tq(2020q3)'{
		
		preserve
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight yieldhat LNtimetomat spread LNbv rating_num coupon_dec if l_p_f==`i' [aw=aum_tot], a(date#fundid) clus(fundid)

		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
						
	
		restore
	
	}
	
	}	
	
postclose coeff


*************************************************
// Investment Grade Bonds
*************************************************

postutil clear

postfile coeff l_p_f date num_obs f_stat _b_yieldhat _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yieldhat _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_ig_qs_pooled_fs.dta", replace
forval i=1(2)3{
		
	forval q=`=tq(2006q1)'(1)`=tq(2020q3)'{
		
		preserve
		
		drop if assetclass == 2
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if l_p_f==`i' & assetclass == 1 [aw=aum_assetclass], a(date#fundid) clus(fundid) resid

		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
						
	
		restore
	
	}
	
	}	
	
postclose coeff

*************************************************
// High Yield Bonds
*************************************************

postutil clear

postfile coeff l_p_f date num_obs f_stat _b_yield _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yield _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_hy_qs_pooled_fs.dta", replace

forval i=1(2)3{
	
	forval q=`=tq(2006q1)'(1)`=tq(2020q3)'{
		
		preserve
		
		drop if assetclass == 1
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if l_p_f==`i' & assetclass == 2 [aw=aum_assetclass], a(date#fundid) clus(fundid) resid
			
		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
		
		restore	
		
	}					
	
	}
	
postclose coeff



****************************************************************************************************
//Graphs
****************************************************************************************************


*************************************************
// All Bonds
*************************************************

use "${revisionoutput}/panel_reg_ts_all_qs_pooled_fs.dta", clear


format %tqCCYY:q date

label var date "Year: Quarter"

sort date

*********************
//Life and Funds
*********************

preserve

local y_var = "yieldhat"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "LNtimetomat"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "LNbv"


local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "spread"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "rating_num"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_all_cb_qs.pdf", as(pdf) replace

restore


*************************************************
// Investment Grade Bonds
*************************************************

use "${revisionoutput}/panel_reg_ts_ig_qs_pooled_fs.dta", clear


format %tqCCYY:q date

label var date "Year: Quarter"

sort date

*********************
//Life and Funds
*********************

preserve

local y_var = "yieldhat"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "LNtimetomat"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "LNbv"


local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "spread"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "rating_num"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_ig_cb_qs.pdf", as(pdf) replace

restore

*************************************************
// High Yield Bonds
*************************************************

use "${revisionoutput}/panel_reg_ts_hy_qs_pooled_fs.dta", clear

format %tqCCYY:q date

label var date "Year: Quarter"

sort date

*********************
//Life and Funds
*********************

preserve

local y_var = "yield"

// winsor2 _b_`y_var' _se_`y_var', cuts(1 99) replace

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "LNtimetomat"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "LNbv"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "spread"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_cb_qs.pdf", as(pdf) replace

restore

preserve

local y_var = "rating_num"

local n num_obs
gen t5 = invttail(`n',0.05)

gen _b_d_5_LB = _b_`y_var' - t5*_se_`y_var'
gen _b_d_5_UB = _b_`y_var' + t5*_se_`y_var'

gen date1 = date + 0.05
gen date2 = date - 0.05

twoway ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==1, lwidth(vthin) lcolor(maroon) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==1, sort msymbol(o) msize(vsmall) mcolor(maroon) cmissing(n)) ///
(rcap _b_d_5_LB _b_d_5_UB date if l_p_f==3, lwidth(vthin) lcolor(edkblue) msize(vsmall) cmissing(n)) ///
(scatter _b_`y_var' date if l_p_f==3, sort msymbol(o) msize(vsmall) mcolor(edkblue) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(2 "Life" 4 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_qs.pdf", as(pdf) replace

twoway ///
(connected _b_`y_var' date if l_p_f==1, lwidth(medium) lpattern("-") lcolor(maroon) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
(connected _b_`y_var' date if l_p_f==3, lwidth(medium) lpattern("-") lcolor(edkblue) msymbol(i) mcolor(edkblue) msize(small) cmissing(n)) ///
, ///
title("", size(medsmall) color(black)) ytitle("Coefficient") ytitle(, size(small)) ///
yline(0, lwidth(thin) lpattern(solid) lcolor(maroon)) ///
ylabel(, format(%5.3f) labsize(small) angle(horizontal) grid glcolor(gs15)) xtitle("Year") xtitle(, size(small)) ///
xlabel(`=tq(2006q1)'(8)`=tq(2020q3)', valuelabel labsize(small)) legend(order(1 "Life" 2 "Mutual Funds (Traditional)")) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) 

graph export "${revisionfigures}/`y_var'_hy_cb_qs.pdf", as(pdf) replace

restore



****************************************************************************************************
// Quarter by Quarter with Pooled First Stages - non-standardized coefficients
****************************************************************************************************


clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"

clear all
set more off
set type double

cap log close

global revisionfigures = "Results/RevisionRFS/Figures"
global revisionoutput = "Results/RevisionRFS/Output"


/* Load data */

use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_non_std.dta", clear

*************************************************
// Investment Grade Bonds
*************************************************

postutil clear

postfile coeff type_new2 date num_obs f_stat _b_yieldhat _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yieldhat _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_ig_qs_pooled_fs_non_std.dta", replace
forval i=1(1)7{
		
	forval q=`=tq(2006q1)'(1)`=tq(2020q3)'{
		
		preserve
		
		drop if assetclass == 2
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 1 [aw=aum_assetclass], a(fundid_ac) resid

		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
						
	
		restore
	
	}
	
	}	
	
postclose coeff

postutil clear

postfile coeff type_new2 date num_obs f_stat _b_yieldhat _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yieldhat _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_ig_qs_pooled_fs_non_std_etf.dta", replace
forval i=5(1)5{
		
	forval q=`=tq(2006q1)'(1)`=tq(2020q3)'{
		
		preserve
		
		drop if assetclass == 2
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 1 [aw=aum_assetclass], a(fundid_ac) resid

		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
						
	
		restore
	
	}
	
	}	
	
postclose coeff

*************************************************
// High Yield Bonds
*************************************************

postutil clear

postfile coeff type_new2 date num_obs f_stat _b_yield _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yield _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_hy_qs_pooled_fs_non_std.dta", replace

forval i=1(1)7{
	
	forval q=`=tq(2006q1)'(1)`=tq(2020q3)'{
		
		preserve
		
		drop if assetclass == 1
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 2 [aw=aum_assetclass_ac], a(fundid_ac) clus(fundid_ac) resid
			
		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
		
		restore	
		
	}					
	
	}
	
postclose coeff

postutil clear

postfile coeff type_new2 date num_obs f_stat _b_yield _b_LNtimetomat _b_spread _b_rating_num _b_LNbv _b_coupon_dec _se_yield _se_LNtimetomat _se_spread _se_rating_num _se_LNbv _se_coupon_dec using /// 
"${revisionoutput}/panel_reg_ts_hy_qs_pooled_fs_non_std_etf.dta", replace

forval i=5(1)5{
	
	forval q=`=tq(2012q1)'(1)`=tq(2020q3)'{
		
		preserve
		
		drop if assetclass == 1
			
		keep if date ==`q' 
		
		capture noisily eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 2 [aw=aum_assetclass], a(fundid_ac) resid
			
		
		post coeff (`i') (`q') (e(N)) (e(widstat)) (_b[yieldhat]) (_b[LNtimetomat]) (_b[spread]) (_b[rating_num]) (_b[LNbv]) (_b[coupon_dec]) (_se[yieldhat]) (_se[LNtimetomat]) (_se[spread]) (_se[rating_num]) (_se[LNbv]) (_se[coupon_dec]) 
					 est clear
		
		restore	
		
	}					
	
	}
	
postclose coeff



****************************************************************************************************
****************************************************************************************************
// Estimation at the fund level with Pooled First Stages
****************************************************************************************************
****************************************************************************************************

clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"


****************************************************************************************************
// Within Credit Rating Group at the Fund Level - Pooled First Stage
****************************************************************************************************


/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2.dta", clear

/* get rid of observations with observed life span less than 10% */
drop if observed_life_span <= 0.5

/* get rid of observations with observed holding less than 30% */
drop if observed_holding_mean <= 0.25

/* get rid of observations with time to maturity > 25 years */
 drop if timetomat >= 25 

gen coupon_dec = coupon/100

gen mp = price_eom/100
gen ln_mp  =ln(mp)

/* Generate instrument from pooled first stage */

gen yieldhat = .

reghdfe yield IVme_alt6_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==0, a(date) clus(date) resid

predict yieldhat_type, xbd

replace yieldhat = yieldhat_type if type_new2==0
	
drop yieldhat_type

forval i=1(1)7{
	
	reghdfe yield IVme_alt6_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) resid
	
	predict yieldhat_type, xbd
	
	replace yieldhat = yieldhat_type if type_new2==`i'
	
	drop yieldhat_type

}

label var yieldhat "$\overline{\textit{Yield}}_{n,t}$"  
label var LNtimetomat "$ \textit{Maturity}_{n,t}$" 
label var spread "$\textit{Bid-Ask}_{n,t}$"
label var rating_num "$\textit{Rating}_{n,t}$" 
label var LNbv "$ \textit{Issuance Size}_{n,t}$" 
label var coupon_dec "$\textit{Coupon}_{n,t}$" 

/* Save standardized data */
save "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_yield.dta", replace

/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_yield.dta", clear


/* Estimate second stage at fund level with pooled first stage instrument */

local Mholding = 2000


keep if Nholding_assetclass>=`Mholding' 

forval q = `=tq(2006q1)'(1)`=tq(2020q3)' {

preserve

di %tq `q'

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass>=`Mholding' & assetclass == 1

if _N > 0{

tempfile ss_yield_fund_ig

statsby ///
_b_ss_yield_cons = _b[_cons] _se_ss_yield_cons = _se[_cons] ///
_b_ss_yield_iv = _b[yieldhat] _se_ss_yield_iv = _se[yieldhat] ///
_b_ss_yield_tmt = _b[LNtimetomat] _se_ss_yield_tmt =_se[LNtimetomat] ///
_b_ss_yield_spread = _b[spread] _se_ss_yield_spread =_se[spread] ///
_b_ss_yield_bv = _b[LNbv] _se_ss_yield_bv =_se[LNbv] ///
_b_ss_yield_rating = _b[rating_num] _se_ss_yield_rating =_se[rating_num]  ///
_b_ss_yield_coupon = _b[coupon_dec] _se_ss_yield_coupon =_se[coupon_dec]  ///
obs_yield=e(N) ///
f_k_yield = e(widstat), ///
by(date fundid) saving(`ss_yield_fund_ig', double replace): reg LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust  //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass 

duplicates drop
 
merge n:1 date fundid using `ss_yield_fund_ig'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_fund_`q'_ig_cf_yield.dta", replace

}

restore


preserve

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass>=`Mholding' & assetclass == 2

if _N > 0{

tempfile ss_yield_fund_hy

statsby ///
_b_ss_yield_cons = _b[_cons] _se_ss_yield_cons = _se[_cons] ///
_b_ss_yield_iv = _b[yieldhat] _se_ss_yield_iv = _se[yieldhat] ///
_b_ss_yield_tmt = _b[LNtimetomat] _se_ss_yield_tmt =_se[LNtimetomat] ///
_b_ss_yield_spread = _b[spread] _se_ss_yield_spread =_se[spread] ///
_b_ss_yield_bv = _b[LNbv] _se_ss_yield_bv =_se[LNbv] ///
_b_ss_yield_rating = _b[rating_num] _se_ss_yield_rating =_se[rating_num]  ///
_b_ss_yield_coupon = _b[coupon_dec] _se_ss_yield_coupon =_se[coupon_dec]  ///
obs_yield=e(N) ///
f_k_yield = e(widstat), ///
by(date fundid) saving(`ss_yield_fund_hy', double replace): reg LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass

duplicates drop
 
merge n:1 date fundid using `ss_yield_fund_hy'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_fund_`q'_hy_cf_yield.dta", replace

}

restore

}


/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_yield.dta", clear


/* Estimate second stage at bin level with pooled first stage instrument */

local Mholding = 2000


keep if Nholding_assetclass<`Mholding' 

forval q = `=tq(2006q1)'(1)`=tq(2020q3)' {

preserve

di %tq `q'

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass<`Mholding' & assetclass == 1

if _N > 0{

tempfile ss_yield_bin_ig

statsby ///
_b_ss_yield_cons = _b[_cons] _se_ss_yield_cons = _se[_cons] ///
_b_ss_yield_iv = _b[yieldhat] _se_ss_yield_iv = _se[yieldhat] ///
_b_ss_yield_tmt = _b[LNtimetomat] _se_ss_yield_tmt =_se[LNtimetomat] ///
_b_ss_yield_spread = _b[spread] _se_ss_yield_spread =_se[spread] ///
_b_ss_yield_bv = _b[LNbv] _se_ss_yield_bv =_se[LNbv] ///
_b_ss_yield_rating = _b[rating_num] _se_ss_yield_rating =_se[rating_num]  ///
_b_ss_yield_coupon = _b[coupon_dec] _se_ss_yield_coupon =_se[coupon_dec]  ///
obs_yield=e(N) ///
f_k_yield = e(widstat), ///
by(date bin_assetclass) saving(`ss_yield_bin_ig', double replace): reg LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust  //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass 

duplicates drop
 
merge n:1 date bin_assetclass using `ss_yield_bin_ig'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_ig_cf_yield.dta", replace

}

restore


preserve

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass<`Mholding' & assetclass == 2

if _N > 0{

tempfile ss_yield_bin_hy

statsby ///
_b_ss_yield_cons = _b[_cons] _se_ss_yield_cons = _se[_cons] ///
_b_ss_yield_iv = _b[yieldhat] _se_ss_yield_iv = _se[yieldhat] ///
_b_ss_yield_tmt = _b[LNtimetomat] _se_ss_yield_tmt =_se[LNtimetomat] ///
_b_ss_yield_spread = _b[spread] _se_ss_yield_spread =_se[spread] ///
_b_ss_yield_bv = _b[LNbv] _se_ss_yield_bv =_se[LNbv] ///
_b_ss_yield_rating = _b[rating_num] _se_ss_yield_rating =_se[rating_num]  ///
_b_ss_yield_coupon = _b[coupon_dec] _se_ss_yield_coupon =_se[coupon_dec]  ///
obs_yield=e(N) ///
f_k_yield = e(widstat), ///
by(date bin_assetclass) saving(`ss_yield_bin_hy', double replace): reg LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass 

duplicates drop
 
merge n:1 date bin_assetclass using `ss_yield_bin_hy'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_hy_cf_yield.dta", replace

}

restore

}

/* Combine Funds and Bins */

use "${revisiondata}/estimation/estimation_coeff_se_fund_195_ig_cf_yield.dta", clear

forval q = `=tq(2009q1)'(1)`=tq(2020q3)' {
	append using "${revisiondata}/estimation/estimation_coeff_se_fund_`q'_ig_cf_yield.dta"
}

save "${revisiondata}/estimation/estimation_coeff_se_fund_ig_cf_yield.dta", replace

/* Note: no fund level estimates for HY bonds */


use "${revisiondata}/estimation/estimation_coeff_se_bin_184_ig_cf_yield.dta", clear

forval q = `=tq(2006q2)'(1)`=tq(2020q3)' {
	append using "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_ig_cf_yield.dta"
}

save "${revisiondata}/estimation/estimation_coeff_se_bin_ig_cf_yield.dta", replace


use "${revisiondata}/estimation/estimation_coeff_se_bin_184_hy_cf_yield.dta", clear

forval q = `=tq(2006q2)'(1)`=tq(2020q3)' {
	append using "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_hy_cf_yield.dta"
}

save "${revisiondata}/estimation/estimation_coeff_se_bin_hy_cf_yield.dta", replace



****************************************************************************************************
****************************************************************************************************
// Estimation at the fund level with Pooled First Stages - Price regressions to check consistency
****************************************************************************************************
****************************************************************************************************

clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"


****************************************************************************************************
// Within Credit Rating Group at the Fund Level - Pooled First Stage
****************************************************************************************************


clear all
set more off
set type double

cap log close

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"
global figures = "Results/Figures"
global output = "Results/Output"
global revisionoutput = "Results/RevisionRFS/Output"

/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2.dta", clear

/* get rid of observations with observed life span less than 10% */
drop if observed_life_span <= 0.5

/* get rid of observations with observed holding less than 30% */
drop if observed_holding_mean <= 0.25

/* get rid of observations with time to maturity > 25 years */
 drop if timetomat >= 25 

gen coupon_dec = coupon/100

gen mp = price_eom/100
gen ln_mp  =ln(mp)


/* Generate instrument from pooled first stage */

gen pricehat = .

reghdfe ln_mp IVme_alt6_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==0, a(date#fundid) clus(date) resid

predict pricehat_type, xbd

replace pricehat = pricehat_type if type_new2==0
	
drop pricehat_type

forval i=1(1)7{
	
	reghdfe ln_mp IVme_alt6_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) resid
	
	predict pricehat_type, xbd
	
	replace pricehat = pricehat_type if type_new2==`i'
	
	drop pricehat_type

}
 
label var pricehat "$\overline{\textit{Price}}_{n,t}$"  
label var LNtimetomat "$ \textit{Maturity}_{n,t}$" 
label var spread "$\textit{Bid-Ask}_{n,t}$"
label var rating_num "$\textit{Rating}_{n,t}$" 
label var LNbv "$ \textit{Issuance Size}_{n,t}$" 
label var coupon_dec "$\textit{Coupon}_{n,t}$" 

/* Save data */
save "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_price.dta", replace


/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_price.dta", clear


/* Estimate second stage at fund level with pooled first stage instrument */

local Mholding = 2000


keep if Nholding_assetclass>=`Mholding' 

forval q = `=tq(2006q1)'(1)`=tq(2020q3)' {

preserve

di %tq `q'

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass>=`Mholding' & assetclass == 1

if _N > 0{

tempfile ss_price_fund_ig

statsby ///
_b_ss_price_cons = _b[_cons] _se_ss_price_cons = _se[_cons] ///
_b_ss_price_iv = _b[pricehat] _se_ss_mp_iv = _se[pricehat] ///
_b_ss_price_tmt = _b[LNtimetomat] _se_ss_price_tmt =_se[LNtimetomat] ///
_b_ss_price_spread = _b[spread] _se_ss_price_spread =_se[spread] ///
_b_ss_price_bv = _b[LNbv] _se_ss_price_bv =_se[LNbv] ///
_b_ss_price_rating = _b[rating_num] _se_ss_price_rating =_se[rating_num]  ///
_b_ss_price_coupon = _b[coupon_dec] _se_ss_price_coupon =_se[coupon_dec]  ///
obs_price=e(N) ///
f_k_price = e(widstat), ///
by(date fundid) saving(`ss_price_fund_ig', double replace): reg LNrweight_conditional pricehat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust  //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass 

duplicates drop
 
merge n:1 date fundid using `ss_price_fund_ig'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_fund_`q'_ig_cf.dta", replace

}

restore


preserve

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass>=`Mholding' & assetclass == 2

if _N > 0{

tempfile ss_price_fund_hy

statsby ///
_b_ss_price_cons = _b[_cons] _se_ss_price_cons = _se[_cons] ///
_b_ss_price_iv = _b[pricehat] _se_ss_price_iv = _se[pricehat] ///
_b_ss_price_tmt = _b[LNtimetomat] _se_ss_price_tmt =_se[LNtimetomat] ///
_b_ss_price_spread = _b[spread] _se_ss_price_spread =_se[spread] ///
_b_ss_price_bv = _b[LNbv] _se_ss_price_bv =_se[LNbv] ///
_b_ss_price_rating = _b[rating_num] _se_ss_price_rating =_se[rating_num]  ///
_b_ss_price_coupon = _b[coupon_dec] _se_ss_price_coupon =_se[coupon_dec]  ///
obs_price=e(N) ///
f_k_price = e(widstat), ///
by(date fundid) saving(`ss_price_fund_hy', double replace): reg LNrweight_conditional pricehat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass

duplicates drop
 
merge n:1 date fundid using `ss_price_fund_hy'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_fund_`q'_hy_cf.dta", replace

}

restore

}


/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_price.dta", clear


/* Estimate second stage at bin level with pooled first stage instrument */

local Mholding = 2000


keep if Nholding_assetclass<`Mholding' 

forval q = `=tq(2006q1)'(1)`=tq(2020q3)' {

preserve

di %tq `q'

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass<`Mholding' & assetclass == 1

if _N > 0{

tempfile ss_price_bin_ig

statsby ///
_b_ss_price_cons = _b[_cons] _se_ss_price_cons = _se[_cons] ///
_b_ss_price_iv = _b[pricehat] _se_ss_price_iv = _se[pricehat] ///
_b_ss_price_tmt = _b[LNtimetomat] _se_ss_price_tmt =_se[LNtimetomat] ///
_b_ss_price_spread = _b[spread] _se_ss_price_spread =_se[spread] ///
_b_ss_price_bv = _b[LNbv] _se_ss_price_bv =_se[LNbv] ///
_b_ss_price_rating = _b[rating_num] _se_ss_price_rating =_se[rating_num]  ///
_b_ss_price_coupon = _b[coupon_dec] _se_ss_price_coupon =_se[coupon_dec]  ///
obs_price=e(N) ///
f_k_price = e(widstat), ///
by(date bin_assetclass) saving(`ss_price_bin_ig', double replace): reg LNrweight_conditional pricehat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust  //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass 

duplicates drop
 
merge n:1 date bin_assetclass using `ss_price_bin_ig'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_ig_cf.dta", replace

}

restore


preserve

/* Only keep funds with at least 2000 bonds conditional on credit rating group at a given date */

keep if date == `q' & Nholding_assetclass<`Mholding' & assetclass == 2

if _N > 0{

tempfile ss_price_bin_hy

statsby ///
_b_ss_price_cons = _b[_cons] _se_ss_price_cons = _se[_cons] ///
_b_ss_price_iv = _b[pricehat] _se_ss_price_iv = _se[pricehat] ///
_b_ss_price_tmt = _b[LNtimetomat] _se_ss_price_tmt =_se[LNtimetomat] ///
_b_ss_price_spread = _b[spread] _se_ss_price_spread =_se[spread] ///
_b_ss_price_bv = _b[LNbv] _se_ss_price_bv =_se[LNbv] ///
_b_ss_price_rating = _b[rating_num] _se_ss_price_rating =_se[rating_num]  ///
_b_ss_price_coupon = _b[coupon_dec] _se_ss_price_coupon =_se[coupon_dec]  ///
obs_price=e(N) ///
f_k_price = e(widstat), ///
by(date bin_assetclass) saving(`ss_price_bin_hy', double replace): reg LNrweight_conditional pricehat LNtimetomat spread LNbv rating_num coupon_dec [aw=aum_assetclass], robust //noid 

keep date fundid bin_assetclass type_new2 aum_tot aum_assetclass assetclass 

duplicates drop
 
merge n:1 date bin_assetclass using `ss_price_bin_hy'
drop if _merge !=3
drop _merge 

save "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_hy_cf.dta", replace

}

restore

}

/* Combine Funds and Bins */

use "${revisiondata}/estimation/estimation_coeff_se_fund_195_ig_cf.dta", clear

forval q = `=tq(2009q1)'(1)`=tq(2020q3)' {
	append using "${revisiondata}/estimation/estimation_coeff_se_fund_`q'_ig_cf.dta"
}

save "${revisiondata}/estimation/estimation_coeff_se_fund_ig_cf.dta", replace

/* Note: no fund level estimates for HY bonds */


use "${revisiondata}/estimation/estimation_coeff_se_bin_184_ig_cf.dta", clear

forval q = `=tq(2006q2)'(1)`=tq(2020q3)' {
	append using "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_ig_cf.dta"
}

save "${revisiondata}/estimation/estimation_coeff_se_bin_ig_cf.dta", replace


use "${revisiondata}/estimation/estimation_coeff_se_bin_184_hy_cf.dta", clear

forval q = `=tq(2006q2)'(1)`=tq(2020q3)' {
	append using "${revisiondata}/estimation/estimation_coeff_se_bin_`q'_hy_cf.dta"
}

save "${revisiondata}/estimation/estimation_coeff_se_bin_hy_cf.dta", replace





